package cn.com.lsy.product.lepao.service.impl;

import cn.com.lsy.product.lepao.common.Constant;
import cn.com.lsy.product.lepao.model.dao.LpAccessoryTblMapper;
import cn.com.lsy.product.lepao.model.entities.LpAccessoryTbl;
import cn.com.lsy.product.lepao.model.entities.LpAccessoryTblExample;
import cn.com.lsy.product.lepao.service.IAccessoryService;
import cn.com.lsy.product.lepao.util.CommonFunctionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.List;

@Service
public class AccessoryService implements IAccessoryService {

    @Autowired
    LpAccessoryTblMapper lpAccessoryTblMapper;

    @Override
    public LpAccessoryTbl selectInfo(int id) {

        return lpAccessoryTblMapper.selectByPrimaryKey(id);
    }

    @Override
    public int isUpdateImg(String url) {

        LpAccessoryTblExample example = new LpAccessoryTblExample();
        example.createCriteria().andUrlEqualTo(url);

        List<LpAccessoryTbl> list = lpAccessoryTblMapper.selectByExample(example);

        if (list != null && list.size() > 0) {

            return list.get(0).getId();

        } else {

            //新增图片
            LpAccessoryTbl lpAccessoryTbl = new LpAccessoryTbl();
            try {
                lpAccessoryTbl.setInsertTime(CommonFunctionUtil.getDateTime(Constant.formatType.second));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            lpAccessoryTbl.setUrl(url);
            lpAccessoryTbl.setName("门店贴图");

            if (lpAccessoryTblMapper.insertSelective(lpAccessoryTbl) > 0) {

                return lpAccessoryTbl.getId();

            } else {

                return 0;
            }

        }

    }

    @Override
    public boolean update(LpAccessoryTbl param) {

        if (lpAccessoryTblMapper.updateByPrimaryKeySelective(param) > 0) {

            return true;

        } else {

            return false;
        }
    }

    @Override
    public int insert(LpAccessoryTbl param) {

        try {
            param.setInsertTime(CommonFunctionUtil.getDateTime(Constant.formatType.second));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        if (lpAccessoryTblMapper.insertSelective(param) > 0) {

            return param.getId();

        } else {

            return 0;
        }
    }

    @Override
    public boolean delete(int id) {

        if (lpAccessoryTblMapper.deleteByPrimaryKey(id) > 0) {

            return true;

        } else {

            return false;
        }
    }
}
